package za.org.jcicada.katcp;

/**
 * SensorBoolean.java
 *
 * @author Adrianna Pinska
 *
 * Created on October 28, 2008, 16:57
 */

import za.org.jcicada.katcp.ISensor;
import za.org.jcicada.katcp.ISensorValue;
import za.org.jcicada.katcp.SensorBase;
import za.org.jcicada.katcp.SensorValueBase;

/**
 * A base class for KAT device discrete sensors.
 */
public class SensorDiscrete<E extends Enum<E>> extends SensorBase {

    /**
     * A class for Discrete sensor values.
     */
    static public class DiscreteValue<F extends Enum<F>> extends SensorValueBase {

        /**
         * The float value for this sensor.
         */
        protected F value;

        /**
         * Constructs a DiscreteValue object.
         *
         * @param value  The sensor value (an Enum instance).
         */
        public DiscreteValue(F value){
            super();
            this.value = value;
        }

        /**
         * Constructs a DiscreteValue object.
         *
         * @param timestamp  When the sensor value was recorded.
         * @param status     The sensor status when the value was recorded.
         * @param value      The sensor value.
         */
        public DiscreteValue(long timestamp, ISensorValue.Status status, F value){
            super(timestamp, status);
            this.value = value;
        }

        /**
         * Get the value.
         *
         * @return  An instance of the sensor Enum class.
         */
        public F getValue() {
            return value;
        }
    }

    /**
     * Constructs a discrete sensor object.
     *
     * @param name         The sensor name.
     * @param description  A description of the sensor.
     * @param units        The units of the sensor value.
     * @param values       The values for this discrete sensor.
     */
    public SensorDiscrete(String name, String description, String units, E[] values)
    {
        if (values == null || values.length == 0) {
            throw new IllegalArgumentException("SensorDiscrete requires a non-empty list of values.");
        }

        String[] sValues = new String[values.length];
        for (int i = 0; i < values.length; i++) {
            sValues[i] = values[i].name().toLowerCase();
        }

        init(name, ISensor.Type.DISCRETE, new DiscreteValue<E>(values[0]), description, units, sValues);
    }

    // ISensor Methods

    /**
     * Format a discrete sensor value.
     *
     * @param value  Sensor value to format.
     * @return  The value formatted as a string.
     */
    public String formatValue(ISensorValue value) {
        @SuppressWarnings("unchecked")
		E evalue = ((DiscreteValue<E>) value).getValue();
        return evalue.name().toLowerCase();
    }

}
